package com.gy.hsim.bservice.friendInfo.restful;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gy.hsim.bservice.framework.bean.BaseReponse;
import com.gy.hsim.bservice.framework.bean.QueryFriendReponse;
import com.gy.hsim.bservice.framework.bean.QueryReponse;
import com.gy.hsim.bservice.framework.bean.QueryVerifyReponse;
import com.gy.hsim.bservice.framework.enums.MessageResultEnum;
import com.gy.hsim.bservice.framework.restful.BaseController;
import com.gy.hsim.bservice.friendInfo.bean.Friend;
import com.gy.hsim.bservice.friendInfo.bean.FriendShield;
import com.gy.hsim.bservice.friendInfo.bean.QueryFriend2UsercData;
import com.gy.hsim.bservice.friendInfo.bean.QueryFriendReq;
import com.gy.hsim.bservice.friendInfo.service.FriendInfoService;

/**
 * 
 * @ClassName: FriendInfoController 
 * @Description: 好友信息Controller 
 * @author Lee 
 * @date 2015-1-13 下午5:59:19
 */
@Controller
public class FriendInfoController extends BaseController {

	private static final Logger log = LoggerFactory
			.getLogger(FriendInfoController.class);

	@Autowired
	private FriendInfoService friendInfoService;

	

	/**
	 * 查询个人资料 请求URL: http://localhost:9090/hsim-bservice/queryFriendList
	 * 
	 * @param request
	 * @param response
	 * @return 响应返回实体类，含返回码、响应消息
	 */

	@RequestMapping(value = "/queryFriendList", method = RequestMethod.POST)
	public @ResponseBody
	QueryVerifyReponse queryFriendList(HttpServletRequest request,
			HttpServletResponse response) throws IOException {

		JSONObject object = (JSONObject)request.getAttribute("object");
		
		QueryFriendReq req = JSON.toJavaObject(object, QueryFriendReq.class);
		
		QueryVerifyReponse resp = new QueryVerifyReponse();
		
		List<QueryFriend2UsercData> friendList;
		try {
			friendList = friendInfoService.query(req,resp);
			
			
			if (friendList != null && friendList.size() > 0) {

				resp.setRetCode(MessageResultEnum.EXEC_SUCCESS.getRetCode());

				resp.setMessage(MessageResultEnum.EXEC_SUCCESS.getMessage());

				resp.setRows(friendList);

			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		
			resp.setRetCode(MessageResultEnum.EXEC_FAILD.getRetCode());

			resp.setMessage(MessageResultEnum.EXEC_FAILD.getMessage());
			resp.setVerifyFlag("0");
		}

		
		return resp;

	}
	
	
	/**
	 * 查询好友备注列表 请求URL: http://localhost:9090/hsim-bservice/queryFriendNickNameList
	 * 
	 * @param request
	 * @param response
	 * @return 响应返回实体类，含返回码、响应消息
	 * @Author David 2015-07-13
	 */

	@RequestMapping(value = "/queryFriendNickNameList", method = RequestMethod.POST)
	public @ResponseBody
	QueryVerifyReponse queryFriendNickNameList(HttpServletRequest request,
			HttpServletResponse response) throws IOException {

		JSONObject object = (JSONObject)request.getAttribute("object");
		
		QueryFriendReq req = JSON.toJavaObject(object, QueryFriendReq.class);
		
		QueryVerifyReponse resp = new QueryVerifyReponse();
		
		List<Friend> friendList;
		try {
			friendList = friendInfoService.queryFriendNickName(req,resp);
			
			
			if (friendList != null && friendList.size() > 0) {

				resp.setRetCode(MessageResultEnum.EXEC_SUCCESS.getRetCode());

				resp.setMessage(MessageResultEnum.EXEC_SUCCESS.getMessage());

				resp.setRows(friendList);

			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		
			resp.setRetCode(MessageResultEnum.EXEC_FAILD.getRetCode());

			resp.setMessage(MessageResultEnum.EXEC_FAILD.getMessage());
			resp.setVerifyFlag("0");
		}

		
		return resp;

	}
	
	
	
	
	/**
	 * 根据用户ID和好友ID，返回是否为好友 
	 *  请求URL: http://localhost:9090/hsim-bservice/queryCheckIsUserFriend
	 * 
	 * @param request
	 * @param response
	 * @return 响应返回实体类，含返回码、响应消息 (808:好友关系，807:不是好友关系)
	 * @Author David 2015-07-13
	 */

	@RequestMapping(value = "/queryCheckIsUserFriend", method = RequestMethod.POST)
	public @ResponseBody
	QueryVerifyReponse queryCheckIsUserFriend(HttpServletRequest request,
			HttpServletResponse response) throws IOException {

		JSONObject object = (JSONObject)request.getAttribute("object");
		
		QueryFriendReq req = JSON.toJavaObject(object, QueryFriendReq.class);
		
		QueryVerifyReponse resp = new QueryVerifyReponse();
		
		boolean result = false;
		
		try {
			result = friendInfoService.checkIsUserFriend(req,resp);
			
			if (result) {

				resp.setRetCode(MessageResultEnum.IS_FRIEND.getRetCode());

				resp.setMessage(MessageResultEnum.IS_FRIEND.getMessage());

			}else{
				resp.setRetCode(MessageResultEnum.IS_NO_FRIEND.getRetCode());

				resp.setMessage(MessageResultEnum.IS_NO_FRIEND.getMessage());
			} 
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		
			resp.setRetCode(MessageResultEnum.EXEC_FAILD.getRetCode());

			resp.setMessage(MessageResultEnum.EXEC_FAILD.getMessage());
			resp.setVerifyFlag("0");
		}
		
		return resp;

	}
	
	
	
	
	
	
	/**
	 * 修改好友备注 请求URL: http://localhost:9090/hsim-bservice/updateFriendNickName
	 * 
	 * @param request
	 * @param response
	 * @return 响应返回实体类，含返回码、响应消息
	 */

	@RequestMapping(value = "/updateFriendNickName", method = RequestMethod.POST)
	public @ResponseBody
	BaseReponse updateFriendNickName(HttpServletRequest request,
			HttpServletResponse response) throws IOException {

        JSONObject object = (JSONObject)request.getAttribute("object");
		
		QueryFriendReq req = JSON.toJavaObject(object, QueryFriendReq.class);
		
		BaseReponse resp = new BaseReponse();
		
		boolean issuccess = false;
		try {
			issuccess = friendInfoService.updateFriendNickName(req,resp);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
			resp.setRetCode(MessageResultEnum.EXEC_FAILD.getRetCode());

			resp.setMessage(MessageResultEnum.EXEC_FAILD.getMessage());
		}


		if (issuccess) {

			resp.setRetCode(MessageResultEnum.EXEC_SUCCESS.getRetCode());

			resp.setMessage(MessageResultEnum.EXEC_SUCCESS.getMessage());

		} 


		return resp;

	}
	
	/**
	 * 查询好友备注 请求URL: http://localhost:9090/hsim-bservice/queryFriendRemark
	 * 
	 * @param request
	 * @param response
	 * @return 响应返回实体类，含返回码、响应消息
	 */

	@RequestMapping(value = "/queryFriendRemark", method = RequestMethod.POST)
	public @ResponseBody
	QueryFriendReponse queryFriendRemark(HttpServletRequest request,
			HttpServletResponse response) throws IOException {

        JSONObject object = (JSONObject)request.getAttribute("object");
		
		QueryFriendReq req = JSON.toJavaObject(object, QueryFriendReq.class);
		
		QueryFriendReponse resp = new QueryFriendReponse();
		
		
		try {
			friendInfoService.queryFriendRemark(req,resp);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
			resp.setRetCode(MessageResultEnum.EXEC_FAILD.getRetCode());

			resp.setMessage(MessageResultEnum.EXEC_FAILD.getMessage());
		}


		resp.setRetCode(MessageResultEnum.EXEC_SUCCESS.getRetCode());

		resp.setMessage(MessageResultEnum.EXEC_SUCCESS.getMessage());

		


		return resp;

	}
	
	/**
	 * 查询个人资料 请求URL: http://localhost:9090/hsim-bservice/queryWhoAddMeList
	 * 
	 * @param request
	 * @param response
	 * @return 响应返回实体类，含返回码、响应消息
	 */

	@RequestMapping(value = "/queryWhoAddMeList", method = RequestMethod.POST)
	public @ResponseBody
	QueryReponse queryWhoAddMeList(HttpServletRequest request,
			HttpServletResponse response) throws IOException {

		JSONObject object = (JSONObject)request.getAttribute("object");
		
		QueryFriendReq req = JSON.toJavaObject(object, QueryFriendReq.class);
		
		QueryReponse resp = new QueryReponse();
	
		List<QueryFriend2UsercData> friendList;
		try {
			friendList = friendInfoService.queryWhoAddMeList(req,resp);
			
			if (friendList != null && friendList.size() > 0) {

				resp.setRetCode(MessageResultEnum.EXEC_SUCCESS.getRetCode());

				resp.setMessage(MessageResultEnum.EXEC_SUCCESS.getMessage());

				resp.setRows(friendList);

			} 
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
			resp.setRetCode(MessageResultEnum.EXEC_FAILD.getRetCode());

			resp.setMessage(MessageResultEnum.EXEC_FAILD.getMessage());
		}


		return resp;

	}
	

	/**
	 * 更新/添加个人资料 请求URL: http://localhost:9090/hsim-bservice/addFriend
	 * 
	 * @param request
	 * @param response
	 * @return 响应返回实体类，含返回码、响应消息
	 */

	@RequestMapping(value = "/addFriend", method = RequestMethod.POST)
	public @ResponseBody
	BaseReponse addFriend(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		
		JSONObject object = (JSONObject)request.getAttribute("object");
		
		QueryFriendReq req = JSON.toJavaObject(object, QueryFriendReq.class);
		
		BaseReponse resp = new BaseReponse();
		
		boolean issuccess = false;
		
		try {
			issuccess = friendInfoService.insert(req,resp);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
			resp.setRetCode(MessageResultEnum.EXEC_FAILD.getRetCode());

			resp.setMessage(MessageResultEnum.EXEC_FAILD.getMessage());
		}


		if (issuccess) {

			resp.setRetCode(MessageResultEnum.EXEC_SUCCESS.getRetCode());

			resp.setMessage(MessageResultEnum.EXEC_SUCCESS.getMessage());

		} 

		return resp;

	}
	
	/**
	 * 更新/添加个人资料 请求URL: http://localhost:9090/hsim-bservice/shieldFriendMsg
	 * 
	 * @param request
	 * @param response
	 * @return 响应返回实体类，含返回码、响应消息
	 */

	@RequestMapping(value = "/shieldFriendMsg", method = RequestMethod.POST)
	public @ResponseBody
	BaseReponse shieldFriendMsg(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		
		JSONObject object = (JSONObject)request.getAttribute("object");
		
		QueryFriendReq req = JSON.toJavaObject(object, QueryFriendReq.class);
		
		BaseReponse resp = new BaseReponse();
		
		boolean issuccess = false;
		try {
			issuccess = friendInfoService.shieldFriendMsg(req,resp);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
			resp.setRetCode(MessageResultEnum.EXEC_FAILD.getRetCode());

			resp.setMessage(MessageResultEnum.EXEC_FAILD.getMessage());
		}


		if (issuccess) {

			resp.setRetCode(MessageResultEnum.EXEC_SUCCESS.getRetCode());

			resp.setMessage(MessageResultEnum.EXEC_SUCCESS.getMessage());

		} 


		return resp;

	}
	
	/**
	 * 更新/添加个人资料 请求URL: http://localhost:9090/hsim-bservice/queryShieldFriendMsg
	 * 
	 * @param request
	 * @param response
	 * @return 响应返回实体类，含返回码、响应消息
	 */

	@RequestMapping(value = "/queryShieldFriendMsg", method = RequestMethod.POST)
	public @ResponseBody
	BaseReponse queryShieldFriendMsg(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		
		JSONObject object = (JSONObject)request.getAttribute("object");
		
		QueryFriendReq req = JSON.toJavaObject(object, QueryFriendReq.class);
		
		QueryReponse resp = new QueryReponse();
		
		List<FriendShield> friendList;
		try {
			friendList = friendInfoService.queryShieldFriendMsg(req,resp);
			
			if (friendList != null && friendList.size() > 0) {

				resp.setRetCode(MessageResultEnum.EXEC_SUCCESS.getRetCode());

				resp.setMessage(MessageResultEnum.EXEC_SUCCESS.getMessage());

				resp.setRows(friendList);

			} 
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
			resp.setRetCode(MessageResultEnum.EXEC_FAILD.getRetCode());

			resp.setMessage(MessageResultEnum.EXEC_FAILD.getMessage());
		}

		
		
		

		return resp;

	}
	
	
	
}
